﻿Partial Public Class COAResultWizard

    Public Shared WithEvents Sample_LookupEdit As GridLookUpEdit
    Public Shared Sample_LookupView As GridView = Nothing

    Public Shared Sub PrintCOA(ByVal sender As Object, ByVal e As EventArgs)
        Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)
        Dim reportName As String
        Dim queryName As String
        Dim lotSerialNo As String
        Dim certificateOfAnalysisID As Guid

        If aDataRow IsNot Nothing Then
            reportName = aDataRow("ReportName").ToString
            queryName = aDataRow("QueryName").ToString
            lotSerialNo = aDataRow("LotNmbr").ToString
            certificateOfAnalysisID = Guid.Parse(aDataRow("CertificateOfAnalysisId").ToString)

            Dim report As DevExpress.XtraReports.UI.XtraReport = SCIEF.ActiveFramework.CommonExt.RptNotification.GetReportFromDB(reportName)

            If report IsNot Nothing Then

                Using mquery As MetaQuery = MetaQuery.GetMetaQueryByName(queryName)
                    If mquery IsNot Nothing Then
                        mquery.SQLWhere = String.Format("LotSerialNo = '{0}'", lotSerialNo)
                        mquery.FillResultQuery()
                        report.DataSource = mquery.ResultQuery
                        report.CreateDocument()
                        Dim aFrm As ReportPrintForm = New ReportPrintForm() With {.Text = "Print COA Template"}
                        aFrm.Report = report
                        aFrm.CertificateOfAnalysisId = certificateOfAnalysisID

                        If Not IsDBNull(aDataRow("BinaryData")) Then
                            aFrm.BinaryString = CType(aDataRow("BinaryData"), Byte())
                        End If

                        aFrm.Show()
                    End If
                End Using
            Else
                MessageBox.Show(String.Format("{0}, Not Found in Report Definition. Notification abord", reportName))
            End If
        End If
    End Sub

    Public Shared Sub PreviewCOA(ByVal sender As Object, ByVal e As EventArgs)
        'Me.Cursor = Cursors.WaitCursor
        Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)

        If aDataRow IsNot Nothing AndAlso Not String.IsNullOrEmpty(aDataRow("BinaryData").ToString) Then
            Dim aFrm As ReportPrintForm = New ReportPrintForm() With {.Text = "Print COA Template"}
            aFrm.BinaryString = CType(aDataRow("BinaryData"), Byte())
            aFrm.CertificateOfAnalysisId = Guid.Parse(aDataRow("CertificateOfAnalysisId").ToString)
            aFrm.PreviewPrintedCOA()
        End If

        'Me.Cursor = Cursors.Default
    End Sub

    Public Shared Sub ShowInDesigner(ByVal sender As System.Object, ByVal e As EventArgs)
        Const frmName As String = "COA Builder"

        Dim view As GridView = CType(sender, GridView)
        Dim aRow As DataRow = view.GetFocusedDataRow()

        Dim newForm As ReportBuilder = New ReportBuilder With {.Name = frmName, .Text = frmName, .ShowIcon = True}
        Dim aMyXtraReport As MyXtraReport = New MyXtraReport()

        aMyXtraReport.DocumentTemplateID = Guid.Parse(aRow("DocumentTemplateId_FK").ToString)
        aMyXtraReport.DocumentTemplateName = aRow("DocumentName").ToString
        aMyXtraReport.QueryID_FK = Guid.Parse(aRow("QueryID_FK").ToString)
        newForm.aMyXtraReport = aMyXtraReport

        newForm.ChooseCommandEnable = False
        newForm.ChooseReportEnable = False
        newForm.NewReportEnable = False

        newForm.Show()

    End Sub


    Public Shared Sub COA_MainContextMenuBeforePopup(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim aMenu As GridViewMenu = CType(sender, GridViewMenu)
        Dim row As DataRow = aMenu.View.GetFocusedDataRow()

        If row IsNot Nothing AndAlso IsDBNull(row("BinaryData")) Then
            Dim i As Integer = 0
            'For Each item In aMenu.Items
            While i < aMenu.Items.Count
                If aMenu.Items(i).Caption.Contains("View in PDF") Then
                    aMenu.Items(i).Enabled = False
                End If
                i += 1
            End While

        End If
    End Sub

    Public Shared Sub MasterCustomRowCellEditForEditing(ByVal sender As System.Object, ByVal e As DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs)
        Dim aDetailview As GridView = DirectCast(sender, GridView)
        Dim aRow As DataRow = aDetailview.GetFocusedDataRow()

        'On filtre pas si c'est la ligre de recherche
        If Not aDetailview.FocusedRowHandle.Equals(GridControl.AutoFilterRowHandle) Then
            ' Dim aEdit As RepositoryItemGridLookUpEdit
            ' Dim sqlWhere As String

            If e.Column.FieldName = "LotId_FK" Then
                'Dim SocietyId As Guid

                'If aRow IsNot Nothing AndAlso Not IsDBNull(aRow("ClientId_FK")) Then
                '    SocietyId = If(IsDBNull(aRow), Guid.Empty, CType(aRow("ClientId_FK"), Guid))
                'Else
                '    SocietyId = Guid.Empty
                'End If

                'sqlWhere = String.Format("SocietyId_FK = '{0}'", SocietyId)
                'aEdit = LOVBuilder("LOV_COAResult_Sample", sqlWhere, Nothing, Nothing)
                'aEdit.Name = "LOV_COAResult_Sample"
                'aDetailview.GridControl.RepositoryItems.Add(aEdit)
                'e.RepositoryItem = aEdit
            ElseIf e.Column.FieldName = "QAQCInspLotID_FK" Then
                'Dim SocietyId As Guid

                'If aRow IsNot Nothing AndAlso Not IsDBNull(aRow("ClientId_FK")) Then
                '    SocietyId = If(IsDBNull(aRow), Guid.Empty, CType(aRow("ClientId_FK"), Guid))
                'Else
                '    SocietyId = Guid.Empty
                'End If

                'sqlWhere = String.Format("SocietyId_FK = '{0}'", SocietyId)
                'aEdit = LOVBuilder("LOV_COAResult_Sample", sqlWhere, Nothing, Nothing)
                'aEdit.Name = "LOV_COAResult_Sample"
                'aDetailview.GridControl.RepositoryItems.Add(aEdit)
                'e.RepositoryItem = aEdit

            ElseIf e.Column.FieldName = "TemplateId_FK" Then
                'Dim SocietyId As Guid = Guid.Empty
                'Dim IMRevisionID As Guid = Guid.Empty

                'If aRow IsNot Nothing AndAlso Not IsDBNull(aRow("ClientId_FK")) Then
                '    SocietyId = If(IsDBNull(aRow), Guid.Empty, CType(aRow("ClientId_FK"), Guid))
                'End If

                'If (Sample_LookupView IsNot Nothing) AndAlso Not IsDBNull(Sample_LookupView.GetRowCellValue(Sample_LookupView.FocusedRowHandle, "ItemMasterRevID_FK")) Then
                '    IMRevisionID = CType(Sample_LookupView.GetRowCellValue(Sample_LookupView.FocusedRowHandle, "ItemMasterRevID_FK"), Guid)
                'End If
                'sqlWhere = String.Format("CategoryId_FK = '{0}' AND ClassId_FK = '{1}'", IdRepository.APP_DOCTEMPLATECATEGORY_REPORT, IdRepository.APP_DOCTEMPLATECLASS_COA)
                'sqlWhere = sqlWhere + String.Format(" AND DocumentTemplateId in (SELECT DocumentTemplateID_FK FROM ItemMasterDocument WHERE SocietyID_FK = '{0}' AND ItemMasterRevID_FK = '{1}')", SocietyId, IMRevisionID)

                'aEdit = LOVBuilder("LOV_COAResult_DocTemplate", sqlWhere, Nothing, Nothing)
                'aEdit.Name = "LOV_COAResult_DocTemplate"
                'aDetailview.GridControl.RepositoryItems.Add(aEdit)
                'e.RepositoryItem = aEdit

            End If
        End If
    End Sub

    Public Shared Sub COA_ShownMasterEditor(sender As Object, e As System.EventArgs)
        Dim detailView As GridView = DirectCast(sender, GridView)

        If detailView.FocusedColumn.FieldName = "QAQCInspLotID_FK" Then
            If TypeOf detailView.ActiveEditor Is GridLookUpEdit Then
                Sample_LookupEdit = CType(detailView.ActiveEditor, GridLookUpEdit)
            End If
        End If
    End Sub

    Public Shared Sub COA_LookupEditCloseUp(sender As Object, e As DevExpress.XtraEditors.Controls.CloseUpEventArgs) Handles Sample_LookupEdit.CloseUp
        Dim alookup As GridLookUpEdit = DirectCast(sender, GridLookUpEdit)

        If alookup.Properties.ValueMember = "QAQCInspLotID" Then
            Sample_LookupView = alookup.Properties.View
        End If
    End Sub

    ''
    ''
    '' OutStandingIssues: A faire en Helper - aleblanc 2011-10-26
    ''
    ''
    Public Shared formatType As DevExpress.Utils.FormatType
    ''' <summary>
    ''' use to Build a LOV for our GRID
    ''' </summary>
    ''' <param name="queryName"></param>
    ''' <param name="sqlWhere"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function LOVBuilder(ByVal queryName As String, ByVal sqlWhere As String, ByVal sqlFrom As String, ByVal filter As String) As RepositoryItemGridLookUpEdit
        Dim aEdit As RepositoryItemGridLookUpEdit
        Dim colVisible As Boolean
        Dim hideCol As Boolean
        Dim colname As String
        Dim Column As DevExpress.XtraGrid.Columns.GridColumn
        Dim view As Views.Base.ColumnView

        Using mq As MetaQuery = MetaQuery.GetMetaQueryByName(queryName)
            If Not String.IsNullOrWhiteSpace(sqlWhere) Then
                mq.SQLWhere = sqlWhere
            End If
            '

            'ICI Le DataField filter appliquer quand on a la securité
            If Not String.IsNullOrWhiteSpace(sqlFrom) Then
                mq.SQLFrom = String.Concat(mq.SQLFrom, sqlFrom)
            End If

            If Not String.IsNullOrWhiteSpace(filter) Then
                mq.SQLWhere = String.Concat(mq.SQLWhere, filter)
            End If
            '
            mq.FillResultQuery()
            aEdit = New RepositoryItemGridLookUpEdit() With {.DataSource = mq.ResultQuery, .DisplayMember = mq.ResultQuery.Columns(CommonExt.Base.AppManager.ApplicationOption.DescriptionColumnIndex).ToString, .ValueMember = mq.GUIDField, .PopupFormSize = New Size(800, 300), .NullText = "(none)"}
            aEdit.View.HorzScrollVisibility = Views.Base.ScrollVisibility.Always
            aEdit.View.OptionsView.ColumnAutoWidth = False
            aEdit.View.OptionsView.ShowAutoFilterRow = True
            aEdit.View.OptionsBehavior.ReadOnly = True
            view = aEdit.View
            view.Columns.Clear()
            For Each row As DataRow In mq.MetaColumns.Rows
                colname = row("ColumnName").ToString
                hideCol = False
                If AppManager.ApplicationOption.DescriptionColumnIndex = 2 Then
                    If colname.EndsWith("_ALT") Then
                        hideCol = True
                    End If
                Else
                    If colname.EndsWith("_PRM") Then
                        hideCol = True
                    End If
                End If
                If hideCol = False Then
                    colVisible = CBool(IIf(CInt(row("pVisible")) = 0, False, True))
                    Column = view.Columns.AddField(colname)
                    Column.VisibleIndex = CInt(row("pOrder"))
                    Column.Caption = row("pCaption").ToString
                    Column.Visible = colVisible
                    Column.Width = CInt(row("pWidth"))
                    Column.ToolTip = row("pToolTip").ToString

                    formatType = GetFormatType(row)
                    '
                    Column.DisplayFormat.FormatType = formatType
                    Column.DisplayFormat.FormatString = row("pNumberFormat").ToString
                End If
            Next
            aEdit.DataSource = mq.ResultQuery
            Return aEdit
        End Using
    End Function

    Public Shared Function GetFormatType(ByVal arow As DataRow) As DevExpress.Utils.FormatType
        If (Not String.IsNullOrEmpty(arow("pNumberFormatID_FK").ToString)) Then
            Select Case CType(arow("pNumberFormatID_FK"), Guid)

                Case CommonExt.Base.IdRepository.META_NUMBERFORMAT_CURRENCY, CommonExt.Base.IdRepository.META_NUMBERFORMAT_GENERALNUMBER, CommonExt.Base.IdRepository.META_NUMBERFORMAT_PERCENT, CommonExt.Base.IdRepository.META_NUMBERFORMAT_STANDARD, CommonExt.Base.IdRepository.META_NUMBERFORMAT_SCIENTIFIC
                    Return DevExpress.Utils.FormatType.Numeric

                Case CommonExt.Base.IdRepository.META_NUMBERFORMAT_GENERALDATE, CommonExt.Base.IdRepository.META_NUMBERFORMAT_LONGDATE, CommonExt.Base.IdRepository.META_NUMBERFORMAT_LONGTIME, CommonExt.Base.IdRepository.META_NUMBERFORMAT_MEDIUMDATE, CommonExt.Base.IdRepository.META_NUMBERFORMAT_MEDIUMTIME, CommonExt.Base.IdRepository.META_NUMBERFORMAT_SHORTDATE, CommonExt.Base.IdRepository.META_NUMBERFORMAT_SHORTTIME
                    Return DevExpress.Utils.FormatType.DateTime

                Case CommonExt.Base.IdRepository.META_NUMBERFORMAT_FORMATEXPR
                    Return DevExpress.Utils.FormatType.Custom

                Case Else
                    Return DevExpress.Utils.FormatType.None
            End Select
        Else
            Return DevExpress.Utils.FormatType.None
        End If
    End Function
    ''
    ''
    '' OutStandingIssues: FIN A faire en Helper - aleblanc 2011-10-26
    ''
    ''
End Class
